package com.jiunuan.live.im.router.provider.cluster;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.rpc.*;
import org.apache.dubbo.rpc.cluster.Directory;
import org.apache.dubbo.rpc.cluster.LoadBalance;
import org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker;

import java.util.List;

/**
 * @Author: JiuNuan
 * @CreateTime: 2024-08-16 13:49
 * @Description: dubbo集群选取特定规则
 */

public class ImRouterClusterInvoker<T> extends AbstractClusterInvoker<T> {
    public ImRouterClusterInvoker(Directory<T> directory){
        super(directory);
    }
    @Override
    protected Result doInvoke(Invocation invocation, List list, LoadBalance loadbalance) throws RpcException {
        checkWhetherDestroyed();
        String ip = (String) RpcContext.getContext().get("ip");
        if(StringUtils.isEmpty(ip)){
            throw new RuntimeException("ip is null!");
        }
        // 获取指定的rpc服务提供者的所有ip信息
        List<Invoker<T>> invokers = list(invocation);
        Invoker<T> matchInvoker = invokers.stream().filter(invoker->{
            //拿到我们服务提供者的暴露地址（ip:端口的格式）
            String serverIp = invoker.getUrl().getHost() + ":" + invoker.getUrl().getPort();
            return serverIp.equals(ip);
        }).findFirst().orElse(null);
        if(matchInvoker == null){
            throw new RuntimeException("[ImRouterClusterInvoker] ip is invalid");
        }
        return matchInvoker.invoke(invocation);
    }
}
